<?php


namespace app\admin\controller\mall;


use app\admin\model\MallCollage;
use app\admin\model\MallOrder;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use jianyan\excel\Excel;
use think\App;

/**
 * @ControllerAnnotation(title="虚拟订单")
 */
class VirtualOrder extends AdminController
{
    
    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\MallOrder();
        
    }
    /**
     * @NodeAnotation(title="虚拟订单列表")
     */
    public function virtualOrder()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $where[] = ['order_status', '>', 0];
            $where[] = ['ea_mall_order.is_virtual', '=', 1];
            $count   = $this->model
                ->withJoin(['goods', 'user', 'collage'], 'LEFT')
                ->where($where)
                ->count();
            $list    = $this->model
                ->withJoin(['goods', 'user', 'collage'], 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data    = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            switch ($post[ 'type' ]) {
                case 0:
                    $return = MallOrder::createNewCollage($post);
                    break;
                case 1:
                    $return = MallOrder::fullCollage($post);
                    break;
            }
            if ($return[ 'status' ] == 0) {
                $this->error($return[ 'msg' ]);
            }
            $this->success('生成拼团成功');
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="隐藏拼团")
     */
    public function hide($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->find();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $collageId = $row->collage_id;
            $save      = MallCollage::where('collage_id', $collageId)->update(['status' => 0]);
        } catch (\Exception $e) {
            $this->error('隐藏拼团失败');
        }
        $save ? $this->success('隐藏拼团成功') : $this->error('隐藏拼团失败');
    }
    /**
     * @NodeAnotation(title="显示拼团")
     */
    public function show($id)
    {
        $this->checkPostRequest();
        $row = $this->model->whereIn('id', $id)->find();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $collageId = $row->collage_id;
            $save      = MallCollage::where('collage_id', $collageId)->update(['status' => 1]);
        } catch (\Exception $e) {
            $this->error('显示拼团失败');
        }
        $save ? $this->success('显示拼团成功') : $this->error('显示拼团失败');
    }
//    /**
//     * @NodeAnotation(title="导出")
//     */
//    public function export()
//    {
//        [$page, $limit, $where] = $this->buildTableParames();
//        $header  = [
//            ['订单号', 'order_no'],
//            ['用户名', 'user_name'],
//            ['商品名', 'goods_name'],
//            ['实际付款金额', 'pay_price'],
//            ['订单状态', 'order_status'],
//            ['创建时间', 'create_time'],
//        ];
//        $where[] = ['order_status', '>', 0];
//        $where[] = ['ea_mall_order.is_virtual', '=', 1];
//        $list    = $this->model
//            ->withJoin(['goods', 'user'], 'LEFT')
//            ->where($where)
//            ->limit(100000)
//            ->order($this->sort)
//            ->select()
//            ->toArray();
//        foreach ($list as $key => $value) {
//            $list[ $key ][ 'user_name' ] = $value[ 'user' ][ 'name' ] ?? '';
//            $list[ $key ][ 'pay_price' ] = '￥' . $value[ 'pay_price' ] / 100 ?? 0;
//            switch ($value[ 'order_status' ]) {
//                case 0:
//                    $list[ $key ][ 'order_status' ] = '未支付';
//                    break;
//                case 10:
//                    $list[ $key ][ 'order_status' ] = '拼团中';
//                    break;
//                case 20:
//                    $list[ $key ][ 'order_status' ] = '已取消';
//                    break;
//                case 30:
//                    $list[ $key ][ 'order_status' ] = '拼团成功';
//                    break;
//                case 40:
//                    $list[ $key ][ 'order_status' ] = '拼团失败';
//                    break;
//                case 50:
//                    $list[ $key ][ 'order_status' ] = '已发货';
//                    break;
//                case 60:
//                    $list[ $key ][ 'order_status' ] = '已完成';
//                    break;
//                case 70:
//                    $list[ $key ][ 'order_status' ] = '退款';
//                    break;
//            }
//        }
//        $fileName = '订单列表' . time();
//        return Excel::exportData($list, $header, $fileName, 'xlsx');
//    }
}